package com.my.mvvmrapid.ui.main.fragment.homewan;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import com.my.mvvmrapid.BR;
import com.my.mvvmrapid.R;
import com.my.mvvmrapid.databinding.FragmentHomeWanBinding;
import com.my.mvvmrapid.ui.main.fragment.homewan.adapter.ArticleTopAdapter;
import com.my.mvvmrapid.ui.main.fragment.homewan.adapter.HomeWanBannerAdapter;
import com.my.mvvmrapid.ui.main.fragment.homewan.adapter.TopLineAdapter;
import com.my.mvvmrapid.ui.qrcodescanning.QRCodeScanningActivity;
import com.my.mvvmrapid.view.CustomRefreshHeader;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;
import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.transformer.ZoomOutPageTransformer;
import com.zhongjh.mvvmrapid.base.ui.BaseFragment;
import com.my.mvvmrapid.view.HeartView;

import org.jetbrains.annotations.NotNull;

import androidx.recyclerview.widget.LinearLayoutManager;

/**
 * Created by zhongjh on 2021/4/8.
 */
@SuppressWarnings("ALL")
public class HomeWanFragment extends BaseFragment<FragmentHomeWanBinding, HomeWanViewModel> {

    ArticleTopAdapter mArticleTopAdapter = new ArticleTopAdapter();

    public static HomeWanFragment newInstance() {
        return new HomeWanFragment();
    }

    @Override
    public int initContentView(@NotNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_home_wan;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initParam() {
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void initData() {
        // 添加生命周期观察者
        binding.banner.addBannerLifecycleObserver(this)
                .setIndicator(new CircleIndicator(getContext()));

        // 实现1号店和淘宝头条类似的效果
        binding.bannerTopLine.addBannerLifecycleObserver(this);

        // 自定义刷新
        binding.refreshLayout.setRefreshHeader(new CustomRefreshHeader(getActivity()));

        // 初始化列表
        LinearLayoutManager ms = new LinearLayoutManager(getContext());
        ms.setOrientation(LinearLayoutManager.HORIZONTAL);
        binding.rlContentHorizontal.setLayoutManager(ms);
        binding.rlContent.setLayoutManager(new LinearLayoutManager(getContext()));

        // 初始化toolbar
        binding.toolbar.setTitle("玩Android");
        binding.toolbar.inflateMenu(R.menu.menu_home_wan);
    }

    @Override
    @SuppressWarnings("unchecked")
    public void initViewObservable() {
        binding.refreshLayout.setOnRefreshListener(refreshLayout -> {
            viewModel.init();
            binding.refreshLayout.finishRefresh(1000);
        });

        // toolbar点击事件
        binding.toolbar.setOnMenuItemClickListener(item -> {
            if (item.getItemId() == R.id.menu_scanner) {
                viewModel.accept(QRCodeScanningActivity.startActivity((RxAppCompatActivity) getActivity()));
            }
            return false;
        });

        viewModel.uiChangeObservable.showBanners.observe(this, banners -> binding.banner.setAdapter(new HomeWanBannerAdapter(banners)));

        mArticleTopAdapter.addChildClickViewIds(R.id.animationView);
        mArticleTopAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (view.getId() == R.id.animationView) {
                ((HeartView) view).startFull();
            }
        });
        viewModel.uiChangeObservable.showHotKeys.observe(this, hotkeys -> binding.bannerTopLine
                .setAdapter(new TopLineAdapter(hotkeys))
                .setOrientation(Banner.VERTICAL)
                .setPageTransformer(new ZoomOutPageTransformer())
                .setOnBannerListener((data, position) -> {
                    // 点击事件
//                    Snackbar.make(banner, ((DataBean) data).title, Snackbar.LENGTH_SHORT).show();
//                    LogUtils.d("position：" + position);
                }));

        viewModel.uiChangeObservable.showTopJson.observe(this, articleTops -> {
            mArticleTopAdapter.setList(articleTops);
            binding.rlContent.setAdapter(mArticleTopAdapter);
            binding.rlContentHorizontal.setAdapter(mArticleTopAdapter);
        });


    }
}
